package com.novellotus.iov.carvice.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.FailureBeen;
import com.novellotus.iov.carvice.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2018/2/1.
 */

public class FailureListAdapter extends RecyclerView.Adapter<FailureListAdapter.FailureHolder> {

    private List<FailureBeen> response;
    FailureItemClick failureItemClick;

    public FailureListAdapter(List<FailureBeen> response, FailureItemClick failureItemClick) {
        this.failureItemClick = failureItemClick;
        if (response == null) {
            this.response = new ArrayList<>();
        } else {
            this.response = response;
        }
    }

    @Override
    public FailureHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_failure_record, null);
        return new FailureHolder(view);
    }

    @Override
    public void onBindViewHolder(FailureHolder holder, final int position) {
        if (response.get(position) != null) {
            holder.tv_plate.setText(response.get(position).getPn());
            holder.tv_location.setText(response.get(position).getAddress());
            //0=已取消,1=未处理,2=已处理
            if (response.get(position).getStatus().equals("1")) {
                holder.tv_statue.setText("未处理");
            } else if (response.get(position).getStatus().equals("2")) {
                holder.tv_statue.setText("已处理");
            } else {
                holder.tv_statue.setText("已取消");
            }

            holder.tv_time.setText(TimeUtils.stampTotime(response.get(position).getReportTime() + ""));
            holder.rlt_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    failureItemClick.clickItem(response.get(position));
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return response.size();
    }

    class FailureHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_plate)
        TextView tv_plate;
        @BindView(R.id.tv_location)
        TextView tv_location;
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.tv_statue)
        TextView tv_statue;
        @BindView(R.id.rlt_item)
        RelativeLayout rlt_item;

        FailureHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface FailureItemClick {
        void clickItem(FailureBeen failureBeen);
    }
}
